﻿// -----------------------------------------------------------------------
// <copyright file="ResultsImplementationListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db ResultsImplementationList
    /// </summary>
    public class ResultsImplementationListData
    {
        /// <summary>
        /// Get ResultsImplementationList By Id
        /// </summary>
        /// <param name="resultsImplementationListId">Param resultsImplementationListId</param>
        /// <returns>Return ResultsImplementationList</returns>
        public ResultsImplementationListEN GetResultsImplementationListById(int resultsImplementationListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ResultsImplementationListEN resultsImplementationList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneResultsImplementationListGetById, resultsImplementationListId);
                while (reader.Read())
                {
                    resultsImplementationList = new ResultsImplementationListEN();

                    if (!reader.IsDBNull(0))
                    {
                        resultsImplementationList.ResultsImplementationListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        resultsImplementationList.Name = reader.GetString(1);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return resultsImplementationList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ResultsImplementationList
        /// </summary>
        /// <returns>Return list all ResultsImplementationList</returns>
        public IList<ResultsImplementationListEN> GetResultsImplementationLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ResultsImplementationListEN> lstResultsImplementationList = new List<ResultsImplementationListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneResultsImplementationListGetAll);
                while (reader.Read())
                {
                    ResultsImplementationListEN resultsImplementationList = new ResultsImplementationListEN();

                    if (!reader.IsDBNull(0))
                    {
                        resultsImplementationList.ResultsImplementationListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        resultsImplementationList.Name = reader.GetString(1);
                    }

                    lstResultsImplementationList.Add(resultsImplementationList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstResultsImplementationList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ResultsImplementationList
        /// </summary>
        /// <param name="resultsImplementationList">Paarm ResultsImplementationList</param>
        /// <returns>Insert ResultsImplementationList</returns>
        public int Insert(ResultsImplementationListEN resultsImplementationList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ResultsImplementationList.Name, resultsImplementationList.Name)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneResultsImplementationListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ResultsImplementationList
        /// </summary>
        /// <param name="resultsImplementationList">Param ResultsImplementationList</param>
        public void Update(ResultsImplementationListEN resultsImplementationList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ResultsImplementationList.ResultsImplementationListId, resultsImplementationList.ResultsImplementationListId),
                                    new SqlParameter(Constants.SqlParamerterName.ResultsImplementationList.Name, resultsImplementationList.Name)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneResultsImplementationListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ResultsImplementationList by resultsImplementationListId
        /// </summary>
        /// <param name="resultsImplementationListId">param resultsImplementationListId</param>
        public void Delete(int resultsImplementationListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ResultsImplementationList.ResultsImplementationListId, resultsImplementationListId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneResultsImplementationListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist name
        /// </summary>
        /// <param name="resultsImplementationListId">Param resultsImplementationListId</param>
        /// <param name="name">Param name</param>
        /// <returns>Return true or false</returns>
        public bool Exist(int resultsImplementationListId, string name)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ResultsImplementationList.ResultsImplementationListId, resultsImplementationListId),
                    new SqlParameter(Constants.SqlParamerterName.ResultsImplementationList.Name, name)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneResultsImplementationListExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}